package com.soft.link.impl;

import com.soft.link.mapper.TsMenuMapper;
import com.soft.link.model.*;
import com.soft.link.service.TsMenuService;
import com.soft.link.service.TsRoleMenuService;
import com.soft.link.service.TsUserRoleService;
import com.sun.glass.ui.Menu;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * company 重庆庆云石油工程技术有限责任公司
 * FileName TsMenuServiceImpl
 * Package com.soft.link.impl
 * Description 菜单接口实现类
 * author Administrator
 * create 2018-03-31 22:55
 * version V1.0
 */
@Service
public class TsMenuServiceImpl implements TsMenuService {
    public Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    TsMenuMapper tsMenuMapper;

    @Autowired
    TsUserRoleService tsUserRoleService;

    @Autowired
    TsRoleMenuService tsRoleMenuService;

    /**
     * @param tsRoleMenuList
     * @methodname findTsMenuList
     * @Description 根据角色菜单查询菜单关系
     * @author 黄学乾
     * @create 2018/3/31/031 22:57
     */
    @Override
    public List<TsMenu> findTsMenuList(List<TsRoleMenu> tsRoleMenuList) throws Exception {
        List<TsMenu> tsMenuList = tsMenuMapper.findTsMenuList(tsRoleMenuList);
        return tsMenuList;
    }

    /**
     * @param tsUser 人员基本信息
     * @methodname findTsMenuList
     * @Description 根据人员信息查询人员所属的菜单信息
     * @author 黄学乾
     * @create 2018/3/31/031 23:08
     */
    @Override
    public List<TsMenu> findTsMenuList(TsUser tsUser) throws Exception {
        List<TsMenu> tsMenu = new ArrayList<>();
        if (tsUser != null) {
            List<TsUserRole> tsUserRoles = tsUserRoleService.findTsUserRoleList(tsUser.getUserId());
            if (tsUserRoles != null && tsUserRoles.size() > 0) {
                List<TsRoleMenu> tsRoleMenus = tsRoleMenuService.findTsRoleMenuList(tsUserRoles);
                if (tsRoleMenus != null && tsRoleMenus.size() > 0) {
                    tsMenu = this.findTsMenuList(tsRoleMenus);
                }
            }
        }
        return tsMenu;
    }

    /**
     * @param * @param tsRole
     * @return
     * @throws {如果有异常说明请填写}
     * @methodname tsMenuService
     * @Description {填写方法注释说明}
     * @author 黄学乾
     * @create 2018/4/27/027 11:36
     */
    @Override
    public List<TsMenu> tsMenuService(TsRole tsRole) throws Exception {
        List<TsMenu> tsMenus = new ArrayList<>();
        List<TsRoleMenu> tsRoleMenu = tsRoleMenuService.findTsRoleMenuListByTsRole(tsRole);
        if (tsRoleMenu != null && tsRoleMenu.size() > 0) {
            tsMenus = this.findTsMenuList(tsRoleMenu);
        }
        return tsMenus;
    }

    /**
    * @methodname offSet 
    * @Description {将菜单列表转成菜单树}
    * @author 黄学乾
    * @create 2018/4/27/027 11:56
    * @param tsMenuList 菜单树
    * @throws {如果有异常说明请填写}
    * @return 
    */
    @Override
    public List<TreeView> offSet(List<TsMenu> tsMenuList) throws Exception {
        List<TreeView> treeViewList =  new ArrayList<>() ;
        if(tsMenuList!=null && tsMenuList.size() > 0){
            for(TsMenu tsMenu: tsMenuList){
                TreeView ztreeView = new TreeView();
                ztreeView.setId(tsMenu.getMenuId());
                ztreeView.setText(tsMenu.getMenuName());
                List<TreeView> nodes = getChild(tsMenuList,tsMenu);
                ztreeView.setNodes(nodes);
                treeViewList.add(ztreeView);
            }
        }
        return treeViewList ;
    }

    /**
    * @methodname getChild
    * @Description {填写方法注释说明}
    * @author 黄学乾
    * @create 2018/4/27/027 17:09
    * @param tsMenuList
    * @throws {如果有异常说明请填写}
    * @return
    */
    public List<TreeView> getChild(List<TsMenu> tsMenuList,TsMenu tsMenu){
        List<TreeView> child = new ArrayList<>();
        for(TsMenu tsMenuChild: tsMenuList){
            if(tsMenu.getMenuId().equals(tsMenuChild.getParentMenuId())){
                TreeView treeView =  new TreeView();
                treeView.setId(tsMenuChild.getMenuId());
                treeView.setText(tsMenuChild.getMenuName());
                treeView.setNodes(getChild(tsMenuList,tsMenuChild));
                child.add(treeView);
            }
        }
        return child ;
    }




}
